//
//  BadgeView.m
//  Knowbox
//
//  Created by LiuXubin on 15/1/13.
//  Copyright (c) 2015年 knowin. All rights reserved.
//

#import "SimBadgeView.h"
#import "UIButton+SimAdditions.h"
#import "UIView+SimAdditions.h"
#import "SimDefine.h"

@implementation SimBadgeView

+ (id)pointItem
{
    return [self pointItemWithSize:CGSizeMake(8, 8)];
}

+ (id)pointItemWithSize:(CGSize)size
{
    SimBadgeView *view = [[SimBadgeView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    return view;
}

- (id)init{
    self = [[SimBadgeView alloc] initWithFrame:CGRectMake(0, 0, 14, 14)];
    if (self) {
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEXRGBCOLOR(0xff0000);
        self.titleLabel.adjustsFontSizeToFitWidth = YES;
        self.titleLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
        self.titleLabel.font = [UIFont boldSystemFontOfSize:10];
        self.layer.cornerRadius = self.height/2;
        self.layer.masksToBounds = YES;
        self.adjustsImageWhenHighlighted = NO;
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        self.userInteractionEnabled = NO;
    }
    
    return self;
}

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:NO];
}

- (void)setIndex:(NSInteger)index
{
    if (_index != index) {
        _index = index;
        if (index == 0) {
            [self setTitle:@"" forState:UIControlStateNormal];
            self.width = 0;
        }
        else{
            NSString *unDoNum = [NSString stringWithFormat:@"%zd", _index];
            [self setTitle:unDoNum forState:UIControlStateNormal];
            CGSize numSize = SIM_MULTILINE_TEXTSIZE2(unDoNum, self.titleLabel.font, CGSizeMake(CGFLOAT_MAX, self.height));
            self.width = MAX(self.height, numSize.width+self.height/2);
        }
    }
}


- (void)addToView:(UIView *)view rightMargin:(CGFloat)rightMargin topMargin:(CGFloat)topMargin;
{
    UIView *superView = view;
    self.right = superView.width-rightMargin;
    self.top = topMargin;
    [superView addSubview:self];
}

- (void)setUnreadMessageCount:(NSInteger)unreadMessageCount
{
    if (_unreadMessageCount != unreadMessageCount) {
        _unreadMessageCount = unreadMessageCount;
        if (_unreadMessageCount == 0) {
            [self setTitle:@"" forState:UIControlStateNormal];
            self.width = 0;
        }else{
            NSString *unDoNum = [NSString stringWithFormat:@"%zd", _unreadMessageCount];
            [self setTitle:unDoNum forState:UIControlStateNormal];
            CGSize numSize = SIM_MULTILINE_TEXTSIZE2(unDoNum, self.titleLabel.font, CGSizeMake(CGFLOAT_MAX, self.height));
            self.width = MAX(self.height, numSize.width+self.height/2);
        }
    }
}

@end
